package cn.ncupyh.comptest;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/7/24 9:59
 * @注释:字符串练习
 */
public class TestChar {
    public static void main(String[] args) {
        String s = "hello world";
        System.out.println(s.contains("hell"));
    }
    public static void main2(String[] args) {
        //除去两端空格-->trim
        String str = "    hello    world     ";
        System.out.println(str);
        String trim = str.trim();//除去两端的空格
        System.out.println(trim);
    }
    public static void main1(String[] args) {
        //从下标为int的开始找'char'/"Stirng"
        String a = "abcdabcabcdabc";
                 // 01234567890123
        System.out.println(a.indexOf('a'));//0
        System.out.println(a.indexOf('a',3));//4
        System.out.println(a.indexOf("abc"));//0
        System.out.println(a.indexOf("abc",3));//4
        System.out.println(a.indexOf('e'));//-1

        //从后往前找
        System.out.println(a.lastIndexOf('a'));//11
        System.out.println(a.lastIndexOf("abc"));//11
        System.out.println(a.lastIndexOf('a',6));//4
        System.out.println(a.lastIndexOf("abc",6));//4
        System.out.println(a.lastIndexOf("abc",13));//11
        System.out.println(a.lastIndexOf("abc",1000));//11

        //将值转化为指定的形式valueof
        String b = String.valueOf(123);
        System.out.println(b);
        int i = Integer.valueOf("123");
        System.out.println(i);
        String b1 = String.valueOf(true);
        System.out.println(b1);

        //转化成数组
        String str = "abc";
        char[] chars = str.toCharArray();
        System.out.println(chars);
        String str1 = "123";
        char[] chars1 = str1.toCharArray();
        System.out.println(chars1);

        //交换字符-->对新的对象进行改变的
        String s = "abcabc";
        System.out.println(s.replace('a','p'));
//        System.out.println(s.replaceFirst(1,"as"));
        System.out.println(s.replace("a","ab"));
        System.out.println(s.replaceAll("a","aa"));
        System.out.println(s.replaceFirst("ab","12"));
        System.out.println(s);


        //分割符
        String st1 = "hello world and people";
        String[] s1 = st1.split(" ");
        for (int j = 0; j < s1.length; j++) {
            System.out.println(s1[j]);
        }
        System.out.println("================");
        String[] s2 = st1.split(" ",2);
        for (int j = 0; j < s2.length; j++) {
            System.out.println(s2[j]);
        }

        System.out.println("================");
        String stt = "198.162.10.1";
        String[] split = stt.split("\\.");
        for (int j = 0; j < split.length; j++) {
            System.out.println(split[j]);
        }

        System.out.println("================");
        String stt1 = "198&162-10=1";
        String[] split1 = stt1.split("&|=|-");//|代表或的意思
        for (int j = 0; j < split1.length; j++) {
            System.out.println(split1[j]);
        }

        System.out.println("================");
        String sttt = "name = djx&age = 18";
        String[] splitt = sttt.split("&");
        for (int j = 0; j < splitt.length; j++) {
            System.out.println(splitt[j]);
        }
    }
}
